package cn.virens.config;

import java.io.IOException;

import javax.cache.CacheManager;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.cache.jcache.JCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.session.config.annotation.web.http.EnableSpringHttpSession;

import cn.virens.web.components.session.JCacheCacheSessionRepository;

/**
 * 配置Spring使用Ecache作为缓存
 * 
 * @作者   :loioi
 * @创建时间 :2017年7月17日
 */
@Configuration
@EnableSpringHttpSession
public class SpringCacheEhcacheConfig {

	/**
	 * 缓存配置
	 * 
	 * @return
	 * @throws IOException
	 */
	@Bean("mManagerFactoryBean")
	public JCacheManagerFactoryBean mManagerFactoryBean() throws IOException {
		JCacheManagerFactoryBean factoryBean = new JCacheManagerFactoryBean();
		factoryBean.setCacheManagerUri(new ClassPathResource("ehcache.xml").getURI());

		return factoryBean;
	}

	/**
	 * 缓存管理器
	 * 
	 * @return
	 */
	@Bean("springCacheManager")
	public JCacheCacheManager mCacheManager(@Qualifier("mManagerFactoryBean") CacheManager cacheManager) {
		return new JCacheCacheManager(cacheManager);
	}

	/**
	 * Spring Session
	 * 
	 * @return
	 */
	@Bean
	public JCacheCacheSessionRepository mSessionRepository(@Qualifier("mManagerFactoryBean") CacheManager cacheManager) {
		return new JCacheCacheSessionRepository(cacheManager);
	}

}
